<?php

namespace App\Admin\Controllers\Points;

use App\Models\Points\Setting;
use Encore\Admin\Form\Row;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Callout;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class SettingController extends Controller
{
    use ModelForm;

    public $header = '规则设置';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $content->header($this->header);

        $words = '说明:<br>初始值: 初始分数; <br> 单日封顶值: 积分每日上限(设置为0表示不封顶) <br> 总封顶值: 积分总上限(设置为0表示不封顶) <br> 兑换开关: 开启后可以参与兑换';
        $content->row((new Callout($words))->style('success'));

        $setting = Setting::first();
        $points_initial = 0;
        $points_day_top = 0;
        $points_total_top = 0;
        $points_status = 0;
        if ($setting) {
            $points_initial = $setting->points_initial;
            $points_day_top = $setting->points_day_top;
            $points_total_top = $setting->points_total_top;
            $points_status = $setting->points_status;
        }

        $form = new Form();
        $form->method('post');
        $form->action('save');
        $form->number('points_initial', '初始值')->default($points_initial);
        $form->number('points_day_top', '单日封顶值')->default($points_day_top);
        $form->number('points_total_top', '总封顶值')->default($points_total_top);
        $states = [
            'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('points_status', '兑换开关')
            ->states($states)->default('1')->default($points_status);

        $content->row(new Box('积分设置', $form));

        return $content;
    }

    public function save(Request $request)
    {
        $rules = [
            'points_initial' => 'required',
            'points_day_top' => 'required',
            'points_total_top' => 'required',
            'points_status' => 'required',
        ];

        $params = $this->validate($request,$rules);

        $points_initial = $request->points_initial;
        $points_day_top = $request->points_day_top;
        $points_total_top = $request->points_total_top;
        $points_status = $request->points_status == 'on' ? '1' : '0';

        $setting = Setting::first();

        if ($setting){
            $setting->points_initial = $points_initial;
            $setting->points_day_top = $points_day_top;
            $setting->points_total_top = $points_total_top;
            $setting->points_status = $points_status;

            $flag = $setting->update();
        } else {

            $setting = new Setting;
            $setting->points_initial = $points_initial;
            $setting->points_day_top = $points_day_top;
            $setting->points_total_top = $points_total_top;
            $setting->points_status = $points_status;

            $flag = $setting->save();
        }


        if ($flag){
            admin_toastr(trans('admin.update_succeeded'), 'success');
        } else {
            admin_toastr(trans('admin.update_failed'), 'error');
        }

        return back();
    }



}
